#!/bin/bash

# filepath: /d:/shutdown-manager/uninstall.sh

# 定义变量
APP_DIR="/opt/shutdown-manager"
CONFIG_PATH="/etc/shutdown-schedules.json"
CRON_PATH="/etc/crontab"
LOG_PATH="/var/log/shutdown-manager.log"
SERVICE_FILE="/etc/systemd/system/shutdown-manager.service"

echo "开始卸载 shutdown-manager..."

# 检查是否以 root 用户运行
if [ "$EUID" -ne 0 ]; then
  echo "请以 root 用户运行此脚本。"
  exit 1
fi

# 停止服务
echo "停止服务..."
systemctl stop shutdown-manager
systemctl disable shutdown-manager

# 删除 systemd 服务文件
if [ -f "$SERVICE_FILE" ]; then
  echo "删除 systemd 服务文件..."
  rm -f "$SERVICE_FILE"
  systemctl daemon-reload
fi

# 删除项目目录
if [ -d "$APP_DIR" ]; then
  echo "删除项目目录..."
  rm -rf "$APP_DIR"
fi

# 删除配置文件
if [ -f "$CONFIG_PATH" ]; then
  echo "删除配置文件..."
  rm -f "$CONFIG_PATH"
fi

# 删除日志文件
if [ -f "$LOG_PATH" ]; then
  echo "删除日志文件..."
  rm -f "$LOG_PATH"
fi

# 清理 crontab 中的关机计划
if [ -f "$CRON_PATH" ]; then
  echo "清理 crontab 中的关机计划..."
  sed -i '/shutdown -h/d' "$CRON_PATH"
fi

echo "卸载完成！shutdown-manager 已成功移除。"